<?php

namespace app\web\service;

use app\common\logic\CouponLogic;
use app\common\model\commonModel;
use app\common\model\trCoupon;
use app\common\model\trUser;
use app\common\util\ErrorCode;

/**
 * Class Auth
 * @package app\web\service
 * 优惠券服务类
 */
class CouponService
{
    private $returnData;

    public function __construct($data)
    {
        $this->returnData = $data;
    }

    // 获取订单页可使用的优惠券列表
    public function getListByFilter($userId, $param){
        if(!isset($param["money"])){
            return ErrorCode::code("params_not_exist", "money");
        }
        if(empty($param["type"])){
            return ErrorCode::code("params_not_exist", "type");
        }
        $money = floatval($param["money"]);
        if($money) {
            $couponList = CouponLogic::getUsableList($userId, $param["type"], $money);
        }else{
            $couponList = [];
        }
        $this->returnData["data"] = $couponList;
        return $this->returnData;
    }

    // 获取订单可优惠的金额
    public function getCouponAmount($couponId, $param){
        $couponModel = new trCoupon();
        $param["type"] = $param["type"] != '0' ? $param["type"] . ",0" : $param["type"]; // 加入通用券
        $filter = [
            ["id", "=", $couponId],
            ["status", "=", 1],
            ["time_from", "<=", date("Y-m-d")],
            ["time_to", ">=", date("Y-m-d")],
            ["type", "in", $param["type"]], // 1：机场券、2：巴士线路券、3：跨境专车接送券、0通用券、4包车线路券
        ];
        $data = $couponModel->getFields("name,amount,condition,content,time_from,time_to,mold,type", $filter);
        if(empty($data)){
            $this->returnData = ErrorCode::code("fail");
            $this->returnData["msg"] = "优惠券错误";
            return $this->returnData;
        }
        if(commonModel::getCount(userCouponTable, [["coupon_id" ,"=", $couponId], ["user_id", "=", $param["userId"]],["status", "=","2"]])){
            $this->returnData = ErrorCode::code("fail");
            $this->returnData["msg"] = "优惠券已使用";
            return $this->returnData;
        }
        $userModel = new trUser();
        $money = floatval($param["money"]);
        $data["amount"] = floatval($data["amount"]);
        if($data["condition"] == "1" && $userModel->getValue("amount", ["id" => $param["userId"]]) > 0){ // 使用条件：不是新注册用户
            $data["amount"] = 0;
        }else if($data["condition"] == "2" && floatval($data["content"]) > $money){ // 使用条件：订单金额不满足
            $data["amount"] = 0;
        }else if($data["condition"] == "3"){ // 使用条件：不是指定用户
            $user = explode(",", $data["content"]);
            if(empty($user) || !in_array($param["userId"], $user)){
                $data["amount"] = 0;
            }
        }
        return $data;
    }

    // 获取未使用的优惠券列表
    public function getUsableCouponList($userId){
        $couponList = CouponLogic::getUsableList($userId);
        $this->returnData["data"] = $couponList;
        return $this->returnData;
    }

    // 获取已使用的优惠券列表
    public function getUseCouponList($userId){
        $couponList = CouponLogic::getUseList($userId);
        $this->returnData["data"] = $couponList;
        return $this->returnData;
    }

    // 获取已过期优惠券列表
    public function getExpireCouponList($userId){
        $couponList = CouponLogic::getExpireList($userId);
        $this->returnData["data"] = $couponList;
        return $this->returnData;
    }

    //获取被推荐优惠券
    public function getShareCoupon(){
        $couponModel = new trCoupon();
        $data = $couponModel->getFields("*", [['mold', '=', 3], ['status', '=', 1], ["mall_id", '=', request()->mallData["id"]]]);
        if (empty($data)) {
            $this->returnData["msg"] = "优惠券不存在";
            $this->returnData['data'] = '';
        } else {
            $this->returnData['data'] = $data;
        }
        return $this->returnData;
    }

    //用户领取优惠券
    public function userReceiveShareCoupon($userId, $param){
        if (!CouponLogic::addUserCouponByRecommend($userId, $param["id"], $msg)){
            $this->returnData = ErrorCode::code("fail");
            $this->returnData["msg"] = $msg;
        }
        return $this->returnData;
    }
}